﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Renci.iRods.Common;

namespace Renci.iRods.Data
{
    public class Quota : DataItem
    {
        [iRodsColumn(DataObjectAttributes.COL_QUOTA_USER_ID)]
        public long UserID { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_QUOTA_RESC_ID)]
        public long ResourceID { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_QUOTA_LIMIT)]
        public long Limit { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_QUOTA_OVER)]
        public long Over { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_QUOTA_MODIFY_TIME)]
        public DateTime ModifiedTime { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_QUOTA_RESC_NAME)]
        public string ResourceName { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_QUOTA_USER_NAME)]
        public string UserName { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_QUOTA_USER_ZONE)]
        public string UserZone { get; internal set; }

        [iRodsColumn(DataObjectAttributes.COL_QUOTA_USER_TYPE)]
        public string UserType { get; internal set; }

        internal Quota(IRodsConnection connection)
            : base(connection)
        {

        }

        public Quota(IRodsConnection connection, string username, string resourceName)
            : base(connection)
        {
            this.UserName = username;
            this.ResourceName = resourceName;
        }

        public override void Refresh()
        {
            this.Exists = this.Load((q) =>
            {
                q.AddCondition(DataObjectAttributes.COL_QUOTA_USER_NAME, string.Format("='{0}'", this.UserName));
                q.AddCondition(DataObjectAttributes.COL_QUOTA_RESC_NAME, string.Format("='{0}'", this.ResourceName));
            });
        }
    }
}
